package com.carlinfo.back.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.userscenter.users.pojo.AAdmins;

public class AuthFilter implements Filter
{
	Logger logger = LogManager.getLogger();
	@Override
	public void destroy()
	{
		this.logger.info("销毁");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException
	{
		this.logger.info("保安的工作");
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res ;
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("adminsSess");
		if(admins != null)
		{
			chain.doFilter(request, response);
		}else 
		{
			String info = "你想跳墙,不可能,买票去";
			session.setAttribute("info", info);
			String path = request.getContextPath() + "/NoLoginBackServlet?method=login";
			response.sendRedirect(path);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		this.logger.info("初始化");
	}

}
